package Dao.impl;

import BaseQuery.BaseQuery;
import BaseQuery.FoodQuery;
import Bean.Food;
import Dao.IFoodDao;
import Utils.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import vo.PageBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDao implements IFoodDao {
	private QueryRunner queryRunner;

	public FoodDao() {
		queryRunner= JdbcUtils.getQueryRunner();
	}

	public int add(Food food) throws SQLException {
	 String sql = "insert into t_food(foodName, foodExplain, foodImg, foodPrice, businessId, remarks)  values(?,?,?,?,?,?)";
	 return queryRunner.update(sql,food.getFoodName(),
			 food.getFoodExplain(),
			 food.getFoodImg(),
			 food.getFoodPrice(),
			 food.getBusinessId(),
			 food.getRemarks());
	}

	public int update(Food food) throws SQLException {
		String sql = "update t_food set foodName=?, foodExplain=?, foodImg=?, foodPrice=?, businessId=?, remarks=? where foodId=?";

		return queryRunner.update(sql , food.getFoodName(),
				food.getFoodExplain(),
				food.getFoodImg(),
				food.getFoodPrice(),
				food.getBusinessId(),
				food.getRemarks(),
				food.getFoodId());
	}

	public int delete(Integer id) throws SQLException {
		String sql= "delete from t_food where foodId=?";
		return queryRunner.update(sql,id);
	}

	public Food getById(Integer id) throws SQLException {
		String sql="select * from t_food where foodId=?";
		return  queryRunner.query(sql,new BeanHandler<>(Food.class),id);
	}

	public List<Food> getAll() throws SQLException {
		String sql = "select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks from t_food";

		return queryRunner.query(sql,new BeanListHandler<>(Food.class));
	}

	public int getCount(BaseQuery baseQuery) throws SQLException {
		String sql= "select count(foodId) from t_food where 1=1 ";
		List<Object> params=new ArrayList<>();
		FoodQuery query=(FoodQuery) baseQuery;
		if(!StringUtils.isEmpty(query.getFoodName())) {
			sql+="and foodName like concat('%',?,'%')";
			//这个语句是用来匹配这个问号的
			params.add(query.getFoodName());
		}
		return queryRunner.query(sql,new ScalarHandler<Long>(),params.toArray()).intValue();
	}

	@Override
	public List<Food> getByPage(BaseQuery baseQuery) throws SQLException {
		//这里先不用计算分页
		String sql = "select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks from t_food where 1=1 ";

		List<Object> params=new ArrayList<>();
		FoodQuery query=(FoodQuery) baseQuery;
		if(!StringUtils.isEmpty(query.getFoodName())) {
			sql+="and foodName like concat('%',?,'%')";
			params.add(query.getFoodName());
		}
		sql += " ORDER BY foodId DESC limit ?,?";
		params.add(query.getstartIndex());
		params.add(query.getPageCount());
		return queryRunner.query(sql,new BeanListHandler<>(Food.class), params.toArray());
	}

}
